﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ParkManagement
{
    /// <summary>
    /// 里程应用服务类
    /// </summary>
    public class ConsumptionAppService : ApplicationService, IConsumptionAppService
    {
        private readonly IAuthManager _authManager;
        private readonly ConsumptionManager _consumptionManager;
        private readonly IRepository<Car, int> _carRepository;
        private readonly IRepository<Consumption, int> _consumptionRepository;

        public ConsumptionAppService(
            JwtAuthManager authManager,
            ConsumptionManager consumptionManager,
            IRepository<Car, int> carRepository,
            IRepository<Consumption, int> consumptionRepository)
        {
            _authManager = authManager;
            _consumptionManager = consumptionManager;
            _carRepository = carRepository;
            _consumptionRepository = consumptionRepository;
        }

        // 列表
        public async Task<ListResponse<ConsumptionDto>> ListAsync(string plateNo, string travelDate = null)
        {
            // 查询
            var car = await _carRepository.GetAsync(m => m.PlateNo == plateNo);
            var list = await _consumptionRepository
                .Where(m => m.CarId == car.Id)
                .WhereIf(!string.IsNullOrWhiteSpace(travelDate), m => m.TravelDate == DateTime.Parse(travelDate))
                .ToListAsync();
            // 计数
            var count = list.Count;
            // 查询用户名
            //var rpcUserService = new RpcUserServiceRealize();
            var dtos = ObjectMapper.Map<List<Consumption>, List<ConsumptionDto>>(list);
            //for (int i = 0; i < dtos.Count; i++)
            //{
            //    dtos[i].UserName = (await rpcUserService.GetAsync(dtos[i].UserId)).UserName;
            //}
            // 返回结果
            return ListResponse<ConsumptionDto>.Successful(count, dtos);
        }

        // 创建
        public async Task<ActionResponse> CreateAsync(CreateConsumptionDto input)
        {
            var userId = _authManager.GetUserId();
            var car = await _carRepository.GetAsync(m => m.PlateNo == input.PlateNo);
            await _consumptionManager.CreateAsync(userId, car.Id, input.TravelDate, input.TravelDistance, input.Amount, input.GasFilling);
            return ActionResponse.Successful();
        }

        // 更新
        public async Task<ActionResponse> UpdateAsync(UpdateConsumptionDto input)
        {
            var consumption = await _consumptionRepository.GetAsync(input.Id);
            if (!string.IsNullOrWhiteSpace(input.TravelDate))
                consumption.SetTravelDate(DateTime.Parse(input.TravelDate));
            if (input.TravelDistance.HasValue)
                consumption.SetTravelDistance(input.TravelDistance.Value);
            if (input.Amount.HasValue)
                consumption.SetAmount(input.Amount.Value);
            if (input.GasFilling.HasValue)
                consumption.SetGasFilling(input.GasFilling.Value);
            return ActionResponse.Successful();
        }

        // 删除
        public async Task<ActionResponse> DeleteAsync(int id)
        {
            await _consumptionRepository.DeleteAsync(id);
            return ActionResponse.Successful();
        }
    }
}